//
//  LSJiaBanDaiBanVC.m
//  LaSaCivilAffairsDepartment
//
//  Created by 姜晓延 on 2019/8/24.
//  Copyright © 2019 wpkj. All rights reserved.
//

#import "LSJiaBanDaiBanVC.h"
#import "LSContactSelectViewController.h"
#import "LSDatePickViewController.h"
#import "LSContactSelectTableViewModel.h"
@interface LSJiaBanDaiBanVC ()< UITextFieldDelegate, UITextViewDelegate>


@property (nonatomic, assign) BOOL progressY;
@property (nonatomic, assign) BOOL detailY;
@property (nonatomic, strong) NSMutableDictionary *progressDict;
@property (nonatomic, strong) NSMutableDictionary *detailDict;

@property (nonatomic, strong) UIButton *selectBtn;

@property (nonatomic, strong) UIView *bottomVIew;

@property (nonatomic, strong) UIView *jieDianView;

@property (nonatomic, strong) UIView *bohuiView;

@property (nonatomic, strong) UIButton *bohuiBtn;

@property (nonatomic, strong) UIButton *saveBtn;

@property (nonatomic, strong) NSMutableDictionary *paraDict;

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) NSArray *JobList;

@property (nonatomic, assign) BOOL isHaveDian;

@end

@implementation LSJiaBanDaiBanVC
- (NSMutableDictionary *)paraDict {
    if (!_paraDict) {
        _paraDict = [[NSMutableDictionary alloc] init];
    }
    return _paraDict;
}
- (NSMutableDictionary *)detailDict {
    if (!_detailDict) {
        _detailDict = [[NSMutableDictionary alloc] init];
    }
    return _detailDict;
}
- (NSMutableDictionary *)progressDict {
    if (!_progressDict) {
        _progressDict = [[NSMutableDictionary alloc] init];
    }
    return _progressDict;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"详情";
    self.paraDict[@"rejectModelNode"] =@"";
    self.paraDict[@"taskName"] = [ValidateUtils validateNull:self.dict[@"taskName"]];
    self.paraDict[@"option"] = @"确认提交";
    self.paraDict[@"ck_formsubmitflag"] = @"1";
    self.paraDict[@"taskId"] = self.dict[@"taskId"];
    [self getNiWenData];
    [self getProgrssData];
    [self JobListData];
    // Do any additional setup after loading the view.
}
- (void)JobListData{
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:OASjzd param:@{@"typeGroupName":@"userDuty",@"token":[ValidateUtils validateNull:[UserInfoModel sharedInstance].token]} completionHandler:^(id responseObject, NSError *connectionError) {
        if (!connectionError) {
            NSLog(@"返回数据----%@",responseObject);
            self.JobList = [NSArray arrayWithArray:responseObject[@"obj"]];
            UITextField *textField = [self.view viewWithTag:100];
            if (textField) {
                for (NSDictionary *dict in self.JobList) {
                    if ([dict[@"typecode"] isEqualToString:self.detailDict[@"JOB"]]) {
                        textField.text = dict[@"typename"];
                        break;
                    }
                }
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:[NSString stringWithFormat:@"%@",connectionError.localizedDescription]];
        }
    }];
}
#pragma mark - 进度历史
- (void)getProgrssData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:[NSString stringWithFormat:@"%@&token=%@",gobl,[UserInfoModel sharedInstance].token] param:@{@"taskId":[ValidateUtils validateNull:self.dict[@"taskId"]]} completionHandler:^(id responseObject, NSError *connectionError) {
        [hud hideAnimated:true];
        self.progressY = YES;
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            
            if (status.integerValue == 200) {
                [self.progressDict addEntriesFromDictionary:[responseObject[@"data"]firstObject]];
                
            } else {
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
        if (self.detailY && self.progressY) {
           
            [self makeJianBanTableView];
        }
        
    }];
}

- (void)getNiWenData{
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:[NSString stringWithFormat:@"%@&token=%@",blxx,[UserInfoModel sharedInstance].token] param:@{@"procInstId":[ValidateUtils validateNull:self.dict[@"procInstId"]],@"definitionName":[ValidateUtils validateNull:self.dict[@"definitionName"]]} completionHandler:^(id responseObject, NSError *connectionError) {
        [hud hideAnimated:true];
        self.detailY = YES;
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            
            if (status.integerValue == 200) {
                [self.detailDict addEntriesFromDictionary:[responseObject[@"data"]firstObject]];
            } else {
                [self.detailDict addEntriesFromDictionary:self.dict];
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
            }
        } else {
            [self.detailDict addEntriesFromDictionary:self.dict];
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
        if (self.detailY && self.progressY) {
            [self makeJianBanTableView];
        }
        
    }];
}

- (void)makeJianBanTableView{
    UIScrollView *scrollView = [[UIScrollView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-SafeAreaTopHeight)];
    scrollView.backgroundColor = RGBColor(240, 240, 240);
    [self.view addSubview:scrollView];
    self.scrollView = scrollView;
    BOOL isedit = YES;
    if (![self.dict[@"taskName"] isEqualToString:@"发起审批"]) {
        isedit = NO;
    }
    
    UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 50+200)];
    view.backgroundColor = hexColor(ffffff);
    [scrollView addSubview:view];
    
    UIImageView *hongdianImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 10, 10)];
    hongdianImage.image = [UIImage imageNamed:@"xinhao"];
    //        hongdianImage.backgroundColor = [UIColor redColor];
    //        hongdianImage.clipsToBounds = YES;
    //        hongdianImage.layer.cornerRadius = 5;
    [view addSubview:hongdianImage];
    UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10+10+10, 10, 80, 30)];
    titlelabel.font = [UIFont systemFontOfSize:15];
    titlelabel.textColor = hexColor(333333);
    [view addSubview:titlelabel];
    titlelabel.text = @"加班事由:";
    
    UITextView *textView = [[UITextView alloc]initWithFrame:CGRectMake(10, 10+CGRectGetMaxY(titlelabel.frame), ScreenWidth-10-10, 200)];
    textView.delegate = self;
    textView.text = [ValidateUtils validateNull:self.detailDict[@"REASON"]];
    textView.tag = 200;
    textView.font = [UIFont systemFontOfSize:15];
    [view addSubview:textView];
    textView.clipsToBounds = YES;
    textView.layer.borderColor = hexColor(666666).CGColor;
    textView.layer.borderWidth = 1;
    if (isedit) {
        textView.editable = YES;
    }else{
        textView.editable = NO;
    }
    NSArray *names = @[@"职务：",@"加班人数：",@"加班时长：",@"加班开始时间：",@"加班结束时间：",@"是否告知分管领导："];
    NSArray *values = @[[ValidateUtils validateNull:self.detailDict[@"JOB"]],[ValidateUtils validateNull:self.detailDict[@"PERSON_NUM"]],[ValidateUtils validateNull:self.detailDict[@"DURATION"]],[ValidateUtils validateNull:self.detailDict[@"BEGINTIME"]],[ValidateUtils validateNull:self.detailDict[@"ENDTIME"]],[ValidateUtils validateNull:self.detailDict[@"IS_NOTICE"]]];
    CGFloat maxY = CGRectGetMaxY(view.frame)+10;
    for (NSInteger i = 0; i < names.count; i++) {
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, maxY, ScreenWidth, 50)];
        view.backgroundColor = hexColor(ffffff);
        [scrollView addSubview:view];
        
        UIImageView *hongdianImage = [[UIImageView alloc]initWithFrame:CGRectMake(10, 20, 10, 10)];
        hongdianImage.image = [UIImage imageNamed:@"xinhao"];
        [view addSubview:hongdianImage];
        
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10+10+10, 10, 120, 30)];
        titlelabel.font = [UIFont systemFontOfSize:15];
        titlelabel.textColor = hexColor(333333);
        [view addSubview:titlelabel];
        titlelabel.text =names[i];
        
        CGSize size = [titlelabel sizeThatFits:CGSizeMake(MAXFLOAT, 30)];
        titlelabel.frame = CGRectMake(10+10+10, 10, size.width, 30);
        CGFloat maxX = CGRectGetMaxX(titlelabel.frame)+10;
        UITextField *textFiled = [[UITextField alloc]initWithFrame:CGRectMake(maxX, 10, ScreenWidth-10-maxX, 30)];
        textFiled.textColor = hexColor(333333);
        textFiled.font = [UIFont systemFontOfSize:15];
        [view addSubview:textFiled];
        textFiled.delegate = self;
        textFiled.tag = 100+i;
        if (i == names.count -1) {
            if (isedit) {
                UIButton *yesBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                yesBtn.frame = CGRectMake(maxX+10, 10, 60, 30);
                [yesBtn setImage:[UIImage imageNamed:@"danquan-n"] forState:UIControlStateNormal];
                [yesBtn setImage:[UIImage imageNamed:@"danxuan"] forState:UIControlStateSelected];
                [yesBtn setTitle:@"是" forState:UIControlStateNormal];
                [yesBtn setTitle:@"是" forState:UIControlStateSelected];
                yesBtn.titleLabel.font = [UIFont systemFontOfSize:15];
                [yesBtn setTitleColor:hexColor(333333) forState:UIControlStateNormal];
                [yesBtn setTitleColor:hexColor(333333) forState:UIControlStateSelected];
                [yesBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
                [view addSubview:yesBtn];
                
                UIButton *noBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                noBtn.frame = CGRectMake(maxX+10+20+60, 10, 60, 30);
                [noBtn setImage:[UIImage imageNamed:@"danquan-n"] forState:UIControlStateNormal];
                [noBtn setImage:[UIImage imageNamed:@"danxuan"] forState:UIControlStateSelected];
                [noBtn setTitle:@"否" forState:UIControlStateNormal];
                [noBtn setTitle:@"否" forState:UIControlStateSelected];
                noBtn.titleLabel.font = [UIFont systemFontOfSize:15];
                [noBtn setTitleColor:hexColor(333333) forState:UIControlStateNormal];
                [noBtn setTitleColor:hexColor(333333) forState:UIControlStateSelected];
                [noBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0, 10)];
                if ([values[i] isEqualToString:@"0"]) {
                    noBtn.selected = YES;
                    self.selectBtn = noBtn;
                }else{
                    yesBtn.selected = YES;
                    self.selectBtn = yesBtn;
                }
                [noBtn addTarget:self action:@selector(shifoutongzhilingdao:) forControlEvents:UIControlEventTouchUpInside];
                [yesBtn addTarget:self action:@selector(shifoutongzhilingdao:) forControlEvents:UIControlEventTouchUpInside];
                yesBtn.tag = 400;
                noBtn.tag = 401;
                [view addSubview:noBtn];
            }else{
                if ([values[i] isEqualToString:@"0"]) {
                    textFiled.text = @"否";
                }else{
                    textFiled.text = @"是";
                }
            }
        }else{
            if (isedit) {
                textFiled.enabled = YES;
            }else{
                textFiled.enabled = NO;
            }
            if (i != 0) {
                textFiled.text = values[i];
                if (i == 3 || i == 4) {
                    if ([ValidateUtils validateNull:values[i]].length > 10) {
                        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
                        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
                        
                        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[values[i] substringToIndex:10] integerValue]];
                        
                        textFiled.text = [dateFormatter stringFromDate:date];
                        if ( i == 3) {
                            self.detailDict[@"BEGINTIME"] = textFiled.text;
                        }else{
                            self.detailDict[@"ENDTIME"] = textFiled.text;
                        }
                        textFiled.enabled = NO;
                        if (isedit) {
                            textFiled.frame = CGRectMake(maxX, 10, ScreenWidth-10-maxX, 30);
                            textFiled.backgroundColor = hexColor(f0f0f0);
                            
                            textFiled.textAlignment = NSTextAlignmentCenter;
                            
                            UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50+10, 15, 20, 20)];
                            imageView.image = [UIImage imageNamed:@"xiala"];
                            [view addSubview:imageView];
                            
                            UIButton *zhankaiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                            zhankaiBtn.frame = CGRectMake(10+80, 5, ScreenWidth-90, 40);
                            zhankaiBtn.tag = 300+i;
                            [zhankaiBtn addTarget:self action:@selector(xuanzeshijian:) forControlEvents:UIControlEventTouchUpInside];
                            [view addSubview:zhankaiBtn];
                        }
                    }
                }
            
                
            }else{
                textFiled.enabled = NO;
                if (self.JobList.count > 0) {
                    for (NSDictionary *dict in self.JobList) {
                        if ([dict[@"typecode"] isEqualToString:self.detailDict[@"JOB"]]) {
                            textFiled.text = dict[@"typename"];
                            break;
                        }
                    }
                }
               
                if (isedit) {
                    textFiled.frame = CGRectMake(maxX, 10, ScreenWidth-10-maxX, 30);
                    textFiled.backgroundColor = hexColor(f0f0f0);
                    
                    textFiled.textAlignment = NSTextAlignmentCenter;
                    
                    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50+10, 15, 20, 20)];
                    imageView.image = [UIImage imageNamed:@"xiala"];
                    [view addSubview:imageView];
                    
                    UIButton *zhankaiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                    zhankaiBtn.frame = CGRectMake(10+80, 5, ScreenWidth-90, 40);
                    zhankaiBtn.tag = 300+i;
                    [zhankaiBtn addTarget:self action:@selector(xuanzeshijian:) forControlEvents:UIControlEventTouchUpInside];
                    [view addSubview:zhankaiBtn];
                }
            }
        }
       
        UIView *lineViwe = [[UIView alloc]initWithFrame:CGRectMake(0, CGRectGetMaxY(textFiled.frame)+9, ScreenWidth, 1)];
        lineViwe.backgroundColor = hexColor(f0f0f0);
        [view addSubview:lineViwe];
       maxY = CGRectGetMaxY(view.frame);
    }
    #pragma mark - 进度
    NSArray *histListNode = [NSArray arrayWithArray:self.progressDict[@"bpmLogList"]];
    
    UIView *progressHistoryView = [[UIView alloc]initWithFrame:CGRectMake(0, maxY+10, ScreenWidth, 0)];
    progressHistoryView.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:progressHistoryView];
    CGFloat progressY = 0;
    for (NSInteger i = 0; i < histListNode.count; i++) {
        NSDictionary *dict = histListNode[i];
        UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, progressY, ScreenWidth, 10+30+30+30+30+10)];
        [progressHistoryView addSubview:view];
        
        UIImageView *imageview = [[UIImageView alloc]initWithFrame:CGRectMake(10, 10, 20, 20)];
        imageview.image = [UIImage imageNamed:@"wancheng"];
        [view addSubview:imageview];
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10+20+10, 10, ScreenWidth-40-10, 20)];
        titlelabel.font = [UIFont systemFontOfSize:15];
        titlelabel.textColor = hexColor(333333);
        [view addSubview:titlelabel];
        titlelabel.text = [ValidateUtils validateNull:dict[@"task_node"]];
        
        UILabel *titlelabel1 = [[UILabel alloc]initWithFrame:CGRectMake(10+20+10, 10+30, ScreenWidth-40-10, 20)];
        titlelabel1.font = [UIFont systemFontOfSize:15];
        titlelabel1.textColor = [UIColor redColor];
        [view addSubview:titlelabel1];
        NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
        [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
        
        NSDate *date = [NSDate dateWithTimeIntervalSince1970:[[ [ValidateUtils validateNull:dict[@"op_time"]] substringToIndex:10] integerValue]];
        
        titlelabel1.text = [NSString stringWithFormat:@"[处理时间：%@]",[dateFormatter stringFromDate:date]];
        
        UILabel *titlelabel2 = [[UILabel alloc]initWithFrame:CGRectMake(10+20+10, 10+30+30, ScreenWidth-40-10, 20)];
        titlelabel2.font = [UIFont systemFontOfSize:13];
        titlelabel2.textColor = hexColor(666666);
        [view addSubview:titlelabel2];
        titlelabel2.text = [NSString stringWithFormat:@"[操作人：%@]",[ValidateUtils validateNull:dict[@"op_name"]]];
        
        UILabel *titlelabel3 = [[UILabel alloc]initWithFrame:CGRectMake(10+20+10, 10+30+30+30, ScreenWidth-40-10, 20)];
        titlelabel3.font = [UIFont systemFontOfSize:15];
        titlelabel3.textColor = hexColor(4EC2F7);
        [view addSubview:titlelabel3];
        titlelabel3.text = [NSString stringWithFormat:@"处理意见：%@",[ValidateUtils validateNull:dict[@"memo"]]];
        titlelabel3.numberOfLines = 0;
        CGSize size = [titlelabel3 sizeThatFits:CGSizeMake(ScreenWidth-40-10, MAXFLOAT)];
        if (size.height>20) {
            titlelabel3.frame = CGRectMake(10+20+10, 10+30+30+30, ScreenWidth-40-10, size.height);
            view.frame = CGRectMake(0, progressY, ScreenWidth,10+30+30+30+size.height+10 );
        }
        progressY = CGRectGetMaxY(view.frame);
    }
    if (histListNode.count != 0) {
        progressHistoryView.frame =CGRectMake(0, maxY+10, ScreenWidth, progressY+10);
        maxY = CGRectGetMaxY(progressHistoryView.frame);
    }else{
        progressHistoryView.frame =CGRectMake(0, maxY+10, ScreenWidth, 0);
        maxY = CGRectGetMaxY(progressHistoryView.frame)-10;
    }
    #pragma mark - 办理意见
    CGFloat bottomHeight = 0;
    NSArray *arr = [NSArray arrayWithArray:self.progressDict[@"transitionList"]];
    if (arr.count>0) {
        if ([[ValidateUtils validateNull:arr.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
            bottomHeight = 80 ;
        }else{
            bottomHeight = 80+50;
        }
    }else{
        bottomHeight = 80 ;
    }
    if ([ValidateUtils validateNull:self.progressDict[@"histListSize"]].integerValue > 0) {
        bottomHeight = bottomHeight+50;
    }
#pragma mark 办理阅办时图
    self.bottomVIew  = [[UIView alloc]initWithFrame:CGRectMake(0, maxY + 10, ScreenWidth, bottomHeight)];
    self.bottomVIew.backgroundColor = [UIColor whiteColor];
    [scrollView addSubview:self.bottomVIew];
    
    maxY = CGRectGetMaxY(self.bottomVIew.frame);
    UIView *lineview = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 1)];
    lineview.backgroundColor = hexColor(eeeeee);
    [_bottomVIew addSubview:lineview];
    //办理意见
    UIView *yijianView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, 80)];
    yijianView.backgroundColor = hexColor(ffffff);
    yijianView.tag = 10000;
    [_bottomVIew addSubview:yijianView];
    
    UIImageView *hongdianImage1 = [[UIImageView alloc]initWithFrame:CGRectMake(10, 15, 10, 10)];
    hongdianImage1.image = [UIImage imageNamed:@"xinhao"];
    [yijianView addSubview:hongdianImage1];
    
    UILabel *yijianLabel = [[UILabel alloc]initWithFrame:CGRectMake(10+CGRectGetMaxX(hongdianImage.frame), 10, 80, 20)];
    yijianLabel.font = [UIFont systemFontOfSize:15];
    yijianLabel.textColor = hexColor(333333);
    [yijianView addSubview:yijianLabel];
    yijianLabel.text = @"办理意见：";
    UITextView *YiJinatextFiled = [[UITextView alloc]initWithFrame:CGRectMake(10, 10+20+10, ScreenWidth-10-10, 30)];
    YiJinatextFiled.textColor = hexColor(333333);
    YiJinatextFiled.font = [UIFont systemFontOfSize:15];
    [yijianView addSubview:YiJinatextFiled];
    YiJinatextFiled.tag = 9000;
    YiJinatextFiled.delegate = self;
    YiJinatextFiled.clipsToBounds = YES;
//    YiJinatextFiled.leftView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, 10, 30)];
//    YiJinatextFiled.leftViewMode = UITextFieldViewModeAlways;
    YiJinatextFiled.layer.borderColor = hexColor(eeeeee).CGColor;
    YiJinatextFiled.layer.borderWidth = 1;
    UIView *linevew1 = [[UIView alloc]initWithFrame:CGRectMake(0, 79, ScreenWidth, 1)];
    linevew1.backgroundColor = hexColor(eeeeee);
    linevew1.tag = 9001;
    [yijianView addSubview:linevew1];
    
    CGFloat yijianY = CGRectGetMaxY(yijianView.frame);
    
    if (arr.count>0) {
        if (![[ValidateUtils validateNull:arr.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
            self.jieDianView = [[UIView alloc]initWithFrame:CGRectMake(0, yijianY+1, ScreenWidth, 50)];
            self.jieDianView.backgroundColor = [UIColor whiteColor];
            [self.bottomVIew addSubview:self.jieDianView];
            yijianY = CGRectGetMaxY(self.jieDianView.frame);
            NSArray *arr = @[@"下一个节点处理人："];
            for (NSInteger i = 0; i < arr.count; i++) {
                UIView *view = [[UIView alloc]initWithFrame:CGRectMake(0, i*50, ScreenWidth, 50)];
                view.backgroundColor = hexColor(ffffff);
                [self.jieDianView addSubview:view];
                
                UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10, 10, 140, 30)];
                titlelabel.font = [UIFont systemFontOfSize:15];
                titlelabel.textColor = hexColor(333333);
                [view addSubview:titlelabel];
                titlelabel.text = arr[i];
                
                UITextField *textFiled = [[UITextField alloc]initWithFrame:CGRectMake(10+140+10,10, ScreenWidth-20-140-40, 30)];
                textFiled.textColor = hexColor(333333);
                textFiled.font = [UIFont systemFontOfSize:15];
                [view addSubview:textFiled];
                textFiled.delegate = self;
                textFiled.tag = 8000+i;
                textFiled.enabled = NO;
               
                UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(ScreenWidth-50+10,15, 20, 20)];
                
                [view addSubview:imageView];
                imageView.userInteractionEnabled = YES;
                UIButton *zhankaiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
                zhankaiBtn.frame = CGRectMake(10+80, 5, ScreenWidth-90, 40);
               
                imageView.image = [UIImage imageNamed:@"lct"];
                textFiled.placeholder = @"选择处理人";
                [zhankaiBtn addTarget:self action:@selector(chooseJieDianChuLuRen) forControlEvents:UIControlEventTouchUpInside];
               
                [view addSubview:zhankaiBtn];
                UIView *linevew1 = [[UIView alloc]initWithFrame:CGRectMake(0, 49, ScreenWidth, 1)];
                linevew1.backgroundColor = hexColor(eeeeee);
                [view addSubview:linevew1];
            }
            
        }
    }
   #pragma mark - 驳回
    
    if ([ValidateUtils validateNull:self.progressDict[@"histListSize"]].integerValue > 0) {
        self.bohuiView = [[UIView alloc]initWithFrame:CGRectMake(0, yijianY, ScreenWidth, 50)];
        
        [self.bottomVIew addSubview:self.bohuiView];
        UIButton *bohuiBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        bohuiBtn.frame = CGRectMake(10, 10, 80, 30);
        [bohuiBtn setImage:[UIImage imageNamed:@"icon-test"] forState:UIControlStateNormal];
        [bohuiBtn setImage:[UIImage imageNamed:@"fangxuankuang2"] forState:UIControlStateSelected];
        [bohuiBtn setTitleColor:hexColor(333333) forState:UIControlStateNormal];
        [bohuiBtn setTitleColor:hexColor(333333) forState:UIControlStateSelected];
        self.bohuiBtn = bohuiBtn;
        [bohuiBtn setTitle:@"驳回" forState:UIControlStateSelected];
        [bohuiBtn setTitle:@"驳回" forState:UIControlStateNormal];
        [bohuiBtn setImageEdgeInsets:UIEdgeInsetsMake(0, 0, 0,5)];
        [self.bohuiView addSubview:bohuiBtn];
        bohuiBtn.titleLabel.font = [UIFont systemFontOfSize:15];
        [bohuiBtn addTarget:self action:@selector(bohuiBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
        UILabel *titlelabel = [[UILabel alloc]initWithFrame:CGRectMake(10+80+10, 10, 120, 30)];
        titlelabel.font = [UIFont systemFontOfSize:15];
        titlelabel.textColor = hexColor(666666);
        [self.bohuiView addSubview:titlelabel];
        titlelabel.text = @"发起审批" ;
    }
    
    
    self.saveBtn  = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveBtn.backgroundColor = hexColor(FFBC01);
    [self.saveBtn setTitle:@"提交" forState:UIControlStateNormal];
    self.saveBtn.titleLabel.font = [UIFont systemFontOfSize:15];
    self.saveBtn.frame = CGRectMake(20, maxY+20, ScreenWidth-40, 40);
    [scrollView addSubview:self.saveBtn];
    
    self.saveBtn.clipsToBounds = YES;
    self.saveBtn.layer.cornerRadius = 5;
    
    scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.saveBtn.frame)+10);
    [self.saveBtn addTarget:self action:@selector(subData:) forControlEvents:UIControlEventTouchUpInside];
}
#pragma mark - 弹窗
- (void)showSelectControllerForTextField:(UITextField *)textField WithContentArray:(NSArray <NSDictionary *> *)contentArray showCode:(NSString *)showCode requestCode:(NSString *)requestCode {
    UIAlertController *alertC = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    for (int i = 0; i < contentArray.count; i++) {
        [alertC addActionTarget:alertC title:contentArray[i][showCode] color:NAV_COLOR action:^(UIAlertAction *action) {
            textField.text = [ValidateUtils validateNull:contentArray[i][showCode]];
            self.detailDict[@"JOB"] = [ValidateUtils validateNull:contentArray[i][requestCode]];
        }];
    }
    [alertC addCancelActionTarget:alertC color:NAV_COLOR title:@"取消"];
    [self presentViewController:alertC animated:true completion:nil];
}
- (void)bohuiBtnClicked:(UIButton*)button{
    if (button.selected) {
        self.paraDict[@"rejectModelNode"] =@"";
        self.paraDict[@"taskName"] = @"";
        NSArray *arr = [NSArray arrayWithArray:self.progressDict[@"transitionList"]];
        if (arr.count>0) {
            if (![[ValidateUtils validateNull:arr.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
                self.jieDianView.hidden = NO;
                CGRect bohuiFrame = self.bohuiView.frame;
                bohuiFrame.origin.y = bohuiFrame.origin.y+50;
                self.bohuiView.frame = bohuiFrame;
                CGRect frame = self.bottomVIew.frame;
                frame.size.height = frame.size.height+50;
                self.bottomVIew.frame = frame;
                CGRect saveFrame = self.saveBtn.frame ;
                saveFrame.origin.y = saveFrame.origin.y+50;
                self.saveBtn.frame = saveFrame;
                self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.saveBtn.frame)+10);
            }
            
        }
    }else{
        
        NSArray *arr = [NSArray arrayWithArray:self.progressDict[@"histListNode"]];
        if (arr.count != 0) {
            self.paraDict[@"rejectModelNode"] = arr[0][@"task_def_key_"];
            self.paraDict[@"taskName"] = arr[0][@"name_"];
        }else{
            self.paraDict[@"rejectModelNode"] =@"";
            self.paraDict[@"taskName"] = @"";
        }
        NSArray *arr1 = [NSArray arrayWithArray:self.progressDict[@"transitionList"]];
        if (arr1.count>0) {
            if (![[ValidateUtils validateNull:arr1.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
                self.jieDianView.hidden = YES;
                CGRect bohuiFrame = self.bohuiView.frame;
                bohuiFrame.origin.y = bohuiFrame.origin.y-50;
                self.bohuiView.frame = bohuiFrame;
                CGRect frame = self.bottomVIew.frame;
                frame.size.height = frame.size.height-50;
                self.bottomVIew.frame = frame;
                CGRect saveFrame = self.saveBtn.frame ;
                saveFrame.origin.y = saveFrame.origin.y-50;
                self.saveBtn.frame = saveFrame;
                self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.saveBtn.frame)+10);
            }
            
        }
      
        
    }
    button.selected = !button.selected;
}
- (void)xuanzeshijian:(UIButton*)button{
    if (button.tag == 303) {
        //开始时间
        UITextField *textfild = [self.view viewWithTag:103];
        [self showDateSelectControllerForTextField:textfild WithTitle:@"请选择加班开始时间"];
    }else if(button.tag == 304){
        //结束时间
        UITextField *textfild = [self.view viewWithTag:104];
        [self showDateSelectControllerForTextField:textfild WithTitle:@"请选择加班结束时间"];
    }else if(button.tag == 300){
         UITextField *textfild = [self.view viewWithTag:100];
        //职务
           [self showSelectControllerForTextField:textfild WithContentArray:self.JobList showCode:@"typename" requestCode:@"typecode"];
    }
}
- (void)showDateSelectControllerForTextField:(UITextField *)textField WithTitle:(NSString *)title {
    LSDatePickViewController *alertVC = [LSDatePickViewController alertControllerWithTitle:[NSString stringWithFormat:@"%@\n\n\n\n\n\n\n\n\n\n\n\n",title] message:nil preferredStyle:UIAlertControllerStyleActionSheet];
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleDefault handler:nil];
    UIAlertAction *okAction = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        // 获取当前datePicker上面选择的时间
        NSDate *date = alertVC.datePicker.date;
        // NSLog(@"-----%@", date);
        // 创建时间格式化工具
        NSDateFormatter *df = [[NSDateFormatter alloc] init];
        df.dateFormat = @"yyyy-MM-dd HH:mm:ss";
        if (textField.tag == 103) {
            if (![textField.text isEqualToString:@""]) {
                UITextField *endtextfield = [self.view viewWithTag:104];
                NSDate *endDate = [df dateFromString:endtextfield.text];
                if ([date earlierDate:endDate] == endDate) {
                    [MBProgressHUD showTipMessageInWindow:@"加班开始时间不可晚于加班结束时间"];
                    return;
                }
            }
        } else if (textField.tag == 104) {
            if (![textField.text isEqualToString:@""]) {
                 UITextField *begintextfiled = [self.view viewWithTag:103];
                NSDate *beginDate = [df dateFromString:begintextfiled.text];
                if ([date earlierDate:beginDate] == date) {
                    [MBProgressHUD showTipMessageInWindow:@"加班结束时间不可早于加班开始时间"];
                    return;
                }
            }
        }
        NSString *str = [df stringFromDate:date];
        textField.text = [NSString stringWithFormat:@"%@",str];
        textField.backgroundColor = UIColor.whiteColor;
    }];
    [alertVC addAction:cancelAction];
    [alertVC addAction:okAction];
    [self presentViewController:alertVC animated:YES completion:nil];
}
- (void)shifoutongzhilingdao:(UIButton*)button{
    if (self.selectBtn == button) {
        return;
    }
    self.selectBtn.selected = NO;
    button.selected = YES;
    self.selectBtn = button;
    if (self.selectBtn.tag == 400) {
        self.detailDict[@"IS_NOTICE"] = @"1";
    }else{
        self.detailDict[@"IS_NOTICE"] = @"0";
    }
}
- (void)chooseJieDianChuLuRen{
    UITextField *textField = [self.view viewWithTag:8000];
    
    //initWithCompleteHandler
    LSContactSelectViewController *lvc = [[LSContactSelectViewController alloc]initWithCompleteHandler:^(LSContactSelectTableViewModel * _Nonnull model) {
        self.paraDict[@"id"] = model.username;
        textField.text = model.realname;
        
    }];
    HEBaseNavController *nav = [[HEBaseNavController alloc] initWithRootViewController:lvc];
    [self presentViewController:nav animated:true completion:nil];
}
- (void)textViewDidEndEditing:(UITextView *)textView{
     if (textView.tag == 9000) {
          self.paraDict[@"reason"] = textView.text;
          return;
      }
    if (textView.tag == 200) {
           self.detailDict[@"REASON"] = textView.text;
       }
}
- (void)textFieldDidEndEditing:(UITextField *)textField{

    //@[@"职务：",@"加班人数：",@"加班时长：",@"加班开始时间：",@"加班结束时间：",@"是否告知分管领导："]
    if (textField.tag == 101) {
        self.detailDict[@"PERSON_NUM"] = textField.text;
    }else if (textField.tag == 102){
         self.detailDict[@"DURATION"] = textField.text;
    }
}
- (void)textViewDidChange:(UITextView *)textView{
    if (textView.tag == 9000) {
        [textView flashScrollIndicators];   // 闪动滚动条
      static CGFloat maxHeight = 10000000.0f;
      CGRect frame = textView.frame;
      CGSize constraintSize = CGSizeMake(frame.size.width, MAXFLOAT);
      CGSize size = [textView sizeThatFits:constraintSize];
      if (size.height >= maxHeight)
      {
          size.height = maxHeight;
          textView.scrollEnabled = YES;    // 允许滚动
      }
      else
      {
          textView.scrollEnabled = NO;    // 不允许滚动，当textview的大小足以容纳它的text的时候，需要设置scrollEnabed为NO，否则会出现光标乱滚动的情况
      }
        if (size.height > 30) {
            CGRect bottomFrame = self.bottomVIew.frame;
                   bottomFrame.size.height = bottomFrame.size.height+(size.height-frame.size.height);
                    self.bottomVIew.frame = bottomFrame;
                   
                   UIView *yujianView = [self.view viewWithTag:10000];
                   CGRect yijianFrame = yujianView.frame;
                   yijianFrame.size.height = yijianFrame.size.height+(size.height-frame.size.height);
                   yujianView.frame = yijianFrame;
                  
                 textView.frame = CGRectMake(frame.origin.x, frame.origin.y, frame.size.width, size.height);
            UIView *lineVIew = [self.view viewWithTag:9001];
            lineVIew.frame = CGRectMake(0, CGRectGetMaxY(textView.frame)+9, ScreenWidth, 1);
                   CGFloat yijianY = CGRectGetMaxY(yujianView.frame);
            NSArray *arr = [NSArray arrayWithArray:self.progressDict[@"transitionList"]];
            if (arr.count > 0) {
                if ([[ValidateUtils validateNull:arr.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
                           self.jieDianView.frame = CGRectMake(0, yijianY+1, ScreenWidth, 50);
                                                
                       }else{
                            self.jieDianView.frame = CGRectMake(0, yijianY+1, ScreenWidth, 50);
                                              
                       }
            }else{
                self.jieDianView.frame = CGRectMake(0, yijianY+1, ScreenWidth, 50);
            }
                  
                   if ([ValidateUtils validateNull:self.progressDict[@"histListSize"]].integerValue > 0){
                        self.bohuiView.frame = CGRectMake(0,CGRectGetMaxY( self.jieDianView.frame), ScreenWidth, 50);
                   }
            CGRect saveFrame = self.saveBtn.frame ;
                       saveFrame.origin.y = saveFrame.origin.y+size.height-frame.size.height;
                       self.saveBtn.frame = saveFrame;
            self.saveBtn.frame = saveFrame;
             self.scrollView.contentSize = CGSizeMake(0, CGRectGetMaxY(self.saveBtn.frame)+10);
        }
    }
   
}
#pragma mark 提交
- (void)subData:(UIButton*)button{
    [self.view endEditing:YES];
    UITextField *textfield = [self.view viewWithTag:9000];
    if (textfield.text.length == 0) {
        [MBProgressHUD showWarnMessage:@"请输入办理意见！"];
        return;
    }
 
   
    NSArray *arr1 = [NSArray arrayWithArray:self.progressDict[@"transitionList"]];
    if (arr1.count > 0) {
        self.paraDict[@"nextnode"] = arr1.firstObject[@"nextnode"];
        if ([[ValidateUtils validateNull:arr1.firstObject[@"nextnode"]] isEqualToString:@"end"]) {
            self.paraDict[@"id"] = @"";
            if (!self.bohuiBtn.selected) {
                self.paraDict[@"model"] = @"1";
             
            }else{
                self.paraDict[@"model"] = @"3";
            }
        }else{
            if (!self.bohuiBtn.selected) {
                self.paraDict[@"model"] = @"1";
                if ([[ValidateUtils validateNull:self.paraDict[@"id"]] isEqualToString:@""]) {
                    [MBProgressHUD showWarnMessage:@"请选择处理人"];
                    return;
                }
                
            }else{
                self.paraDict[@"model"] = @"3";
                 self.paraDict[@"id"] = @"";
            }
           
        }
    }else{
        self.paraDict[@"nextnode"] = @"";
        self.paraDict[@"model"] = @"1";
        self.paraDict[@"SELECT_TASKNODE"] = @"";
        self.paraDict[@"taskName"] = @"";
        self.paraDict[@"id"] = @"";
        
    }
    self.paraDict[@"nextCodeCount"] = [ValidateUtils validateNull:self.progressDict[@"nextCodeCount"]];
   if ([[ValidateUtils validateNull:self.detailDict[@"REASON"]] isEqualToString:@""]) {
       [MBProgressHUD showTipMessageInWindow:@"请输入出差事由"];
       return;
   }  else if ([[ValidateUtils validateNull:self.detailDict[@"PERSON_NUM"]] isEqualToString:@""]) {
       [MBProgressHUD showTipMessageInWindow:@"请输入加班人数"];
       return;
   } else if ([[ValidateUtils validateNull:self.detailDict[@"DURATION"]] isEqualToString:@""]) {
       [MBProgressHUD showTipMessageInWindow:@"请输入加班时长"];
       return;
   }
    
    NSLog(@"参数%@",self.paraDict);
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
    NSDictionary*dict= @{@"id":[ValidateUtils validateNull:self.detailDict[@"ID"]],@"job":[ValidateUtils validateNull:self.detailDict[@"JOB"]],@"realName":[ValidateUtils validateNull:self.detailDict[@"REAL_NAME"]],@"personNum":[ValidateUtils validateNull:self.detailDict[@"PERSON_NUM"]],@"reason":[ValidateUtils validateNull:self.detailDict[@"REASON"]],@"isNotice":[ValidateUtils validateNull:self.detailDict[@"IS_NOTICE"]],@"duration":[ValidateUtils validateNull:self.detailDict[@"DURATION"]],@"begintime":[ValidateUtils validateNull:self.detailDict[@"BEGINTIME"]],@"endtime":[ValidateUtils validateNull:self.detailDict[@"ENDTIME"]]};
    
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:[NSString stringWithFormat:@"%@&token=%@",jbdoUpdate,[UserInfoModel sharedInstance].token] param:dict completionHandler:^(id responseObject, NSError *connectionError) {
        hud.hidden = YES;
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            
            if (status.integerValue == 200) {
                 [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
               
            } else {
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
         [self subMidAllData];
    }];
    
    
    
}
-(NSData*)readFileData:(NSString *)path{
    NSFileHandle *handle = [NSFileHandle fileHandleForReadingAtPath:path];
    NSData *fileData = [handle readDataToEndOfFile];
    [handle closeFile];
    return fileData;
}


- (void)subMidAllData{
    
    
    MBProgressHUD *hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES];
    [NetworkUtils requestNOHandleWithMethod:@"POST" URL:[NSString stringWithFormat:@"%@&token=%@",dobl,[UserInfoModel sharedInstance].token] param:self.paraDict completionHandler:^(id responseObject, NSError *connectionError) {
        hud.hidden = YES;
        if (!connectionError) {
            NSNumber *status = [responseObject objectForKey:@"status"];
            
            if (status.integerValue == 200) {
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
                [self.navigationController popViewControllerAnimated:YES];
                
            } else {
                [MBProgressHUD showTipMessageInWindow:responseObject[@"message"]];
            }
        } else {
            [MBProgressHUD showTipMessageInWindow:connectionError.localizedDescription];
        }
    }];
}
- (BOOL)textView:(UITextView *)textView shouldChangeTextInRange:(NSRange)range replacementText:(NSString *)text {
    if (textView.tag == 200) {
        if (range.location > 60) {
            return NO;
        } else {
            if (([ValidateUtils isInputRuleNotBlank:text] && ![ValidateUtils hasSpecialString:text]) || [text isEqualToString:@""]) {//当输入符合规则和退格键时允许改变输入框
                return YES;
            } else {
                return NO;
            }
        }
    }
    return true;
}

- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
    if (textField.tag != 9000) {
        if (string.length == 0 ||  [string isEqualToString:@""]) return true;// 如果是删除字符，肯定是要通过的
          
          /*
           * 不能输入.0-9以外的字符。
           * 设置输入框输入的内容格式
           * 只能有一个小数点
           * 小数点后最多能输入两位
           * 如果第一位是.则前面加上0.
           * 如果第一位是0则后面必须输入点，否则不能输入。
           */
          
          // 判断是否有小数点
          if ([textField.text containsString:@"."]) {
              self.isHaveDian = true;
          }else{
              self.isHaveDian = false;
          }
          if (string.length > 0) {
              //当前输入的字符
              unichar single = [string characterAtIndex:0];
              // 不能输入.0-9以外的字符
              if (!((single >= '0' && single <= '9') || single == '.'))
              {
                  //            [MBProgressHUD bwm_showTitle:@"您的输入格式不正确" toView:self hideAfter:1.0];
                  return NO;
              }
              
              // 只能有一个小数点
              if (self.isHaveDian && single == '.') {
                  return NO;
              }
              
              // 如果第一位是.则前面加上0.
              if ((textField.text.length == 0) && (single == '.')) {
                  textField.text = @"0";
              }
              
              // 如果第一位是0则后面必须输入点，否则不能输入。
              if ([textField.text hasPrefix:@"0"]) {
                  if (textField.text.length > 1) {
                      NSString *secondStr = [textField.text substringWithRange:NSMakeRange(1, 1)];
                      if (![secondStr isEqualToString:@"."]) {
                          //                    [MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                          return NO;
                      }
                  }else{
                      if (![string isEqualToString:@"."]) {
                          //                    [MBProgressHUD bwm_showTitle:@"第二个字符需要是小数点" toView:self hideAfter:1.0];
                          return NO;
                      }
                  }
              }
              
              // 小数点后最多能输入一位
              if (self.isHaveDian) {
                  NSRange ran = [textField.text rangeOfString:@"."];
                  // 由于range.location是NSUInteger类型的，所以这里不能通过(range.location - ran.location)>2来判断
                  if (range.location > ran.location) {
                      if ([textField.text pathExtension].length > 1) {
                          //                    [MBProgressHUD bwm_showTitle:@"小数点后最多有两位小数" toView:self hideAfter:1.0];
                          return NO;
                      }
                  }
              }
          }
          if (range.length == 1 && string.length == 0) {
              return YES;
          } else {
              //否则返回yes,不限制其他textfield
              if ((textField.tag == 101 && textField.text.length >= 5) ||(textField.tag == 102 && textField.text.length >= 5)) {
                  textField.text = [textField.text substringToIndex:5];
                  return NO;
              }
          }
          
          if (textField.tag == 102 ) {
              return [self validateNumberAndDot:string];
          }
          if (textField.tag == 101) {
              return [self validateNumber:string];
          }
    }
    return YES;
}

- (BOOL)validateNumber:(NSString*)number {
    BOOL res =YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789"];
    int i =0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i,1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length ==0) {
            res =NO;
            break;
        }
        i++;
    }
    return res;
}

- (BOOL)validateNumberAndDot:(NSString*)number {
    BOOL res =YES;
    NSCharacterSet* tmpSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789."];
    int i =0;
    while (i < number.length) {
        NSString * string = [number substringWithRange:NSMakeRange(i,1)];
        NSRange range = [string rangeOfCharacterFromSet:tmpSet];
        if (range.length ==0) {
            res =NO;
            break;
        }
        i++;
    }
    return res;
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
